import * as React from 'react';
import * as ReactDOM from 'react-dom';
import * as Global from '../TS/Global';
import * as Mycss from "../CSS/mycss.css";
import * as MyCfg from './MyCfg';
import * as Loading from './Loading';
import { MessageBox } from './messagebox';
export interface CfgRevProps extends Global.Myprops {
    ttxx?: string
};

interface CfgRevState { };

export class CfgRev extends React.Component<CfgRevProps,
    CfgRevState> {
    constructor(props) {
        super(props);
        //设置状态
        this.state = {
            // secondsElapsed: 0
        };

    }
    GetWeb(): void {
        console.log("执行命令");
        let promise = new Promise(function (resolve, reject) {

            $.ajax({
                url: "WebService.cgi",

                type: 'post',
                error: (xhr, status, error) => {

                    reject(error);
                },
                contentType: "application/x-www-form-urlencoded",
                data: {
                    cmd: 'GetCfg'
                },
                dataType: "json",
                // dataType: "jsonp", jsonp: "callback", jsonpCallback: "success_jsonpCallback",
                success: (result) => {
                    resolve(result);
                }
            });
        });
        promise.then((results: any) => {
            console.log("获取数据成功");
            let control: any = this.refs.loading;
            control.ReciveMsgEvent(this, "close", "");
            //this.forceUpdate();
            console.log(results)
        }).catch((errors) => {

            let control: any = this.refs.loading;
            control.ReciveMsgEvent(this, "close", "");
            console.log("获取数据失败");
            let control_msg: any = this.refs.messagebox;
            control_msg.ShowError("获取配置信息出错:" + errors);
        });
    }
    //用于处理收到的数据
    ReciveMsgEvent(source: any, cmd: string, obj?: any, ...args) {
        if (cmd == 'updatecfg') {
            this.forceUpdate();
        } else { }
    }

    //下一步进行显示 进行初始化
    componentDidMount() {
        //类似window.onload
        this.setState((prv, prop) => ({
            // count: this.state.count + 1
        }));
    }
    ShowFactory() {
        let confirm: MessageBox = this.refs.confirm as MessageBox;
        confirm.Show();
    }
    ReFactoryCfg() {
        console.log("ReFactoryCfg");
        this
            .props
            .OnMessageSendEvent(this, '', "Factory");
    }
    componentWillReceiveProps(nextProps) {
        //每次父控件重新绘制,赋值新的props触发
        this.setState({});
    }
    //处理点击
    public handleClick(event: any): void {
        event.preventDefault();
        let name = event.currentTarget.name;
        if (name == "setfactory") {
            this.ShowFactory();
        }

    }
    //处理页面变换
    public handleChange(event: any): void {
        let tcfg: any = {};
        let value: any;
        let isset: boolean = false;
        if (event.target.type == "checkbox") {

            tcfg[event.target.name] = event.target.checked;
            isset = true;
        } else if (event.target.type == "radio") {
            if (event.target.checked) {

                tcfg[event.target.name] = event.target.value;
                isset = true;
            }

        } else if (event.target.type == "select-one") {
            // <option value='0'>全部</option>
            var index = event.target.selectedIndex;
            value = event.target.options[index].value;

        } else {
            value = event.target.value;
        }
        if (!isset) {

            tcfg[event.target.name] = value;
        }
        this.setState(tcfg, () => { });
    }
    //处理按键按下事件
    public handleKeyDown(event: React.KeyboardEvent<HTMLInputElement>): void {
        if (event.keyCode == 13) {
            //处理回车事件
        }
    }

    //进行渲染
    public render(): JSX.Element {
        return (
            <div className="col-xs-12 col-sm-10">
                <div className="form-group">

                    {/* <button type="button" className="btn btn-primary">button</button> */}
                    <br />
                    ---------------------------------
                    <br />
                    <a className="btn btn-primary" href = "cfg.tar" >  {Global.GetLocal("下载配置文件")}</a>
                    <br />
                    ---------------------------------
                    <br />
                    <a name="setfactory" className="btn btn-primary" onClick={e => this.handleClick(e)} >  {Global.GetLocal("恢复出厂设置")}</a>
                    <MessageBox
                        ref="confirm"
                        MessageType='confirm'
                        Title={Global.GetLocal("警告")}
                        Content={Global.GetLocal("确认恢复出厂设置吗")}
                        Ok={() => {
                            this.ReFactoryCfg()
                        }} />
                </div>

                <form method="post" action="/WebService.cgi" encType="multipart/form-data">
                    <div className="form-group">

                        <input type="hidden" name="datafrom" value="web" />
                        <input type="hidden" name="webcmd" value="uploadcfg" />
                        <label className=" control-label">{Global.GetLocal("上传本地备份的配置文件")}</label>
                        <input type="file" className="form-control" id="" placeholder={Global.GetLocal("上传文件")}
                            name="userfile" accept="aplication/zip" />
                    </div>
                    <button type="submit" className="btn btn-primary">{Global.GetLocal("确定")}</button>
                </form>
            </div >
        );
    }
}